package pos.data;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

import com.mysql.jdbc.PreparedStatement;

import pos.domain.*;

public class JDBCObjetoDAO implements IObjetoDAO{
	
	public List selectAllObjetos() {
        Connection conn = ConnectionManager.getInstance().checkOut();

        PreparedStatement stmt = null;
        List searchResults = new LinkedList();
        ResultSet result = null;

        try {

            String sql = "SELECT * FROM Objeto";
            stmt = (PreparedStatement)conn.prepareStatement(sql); 
            stmt.executeQuery();
            result = stmt.executeQuery();

            while (result.next()) {
            	
                Objeto temp = new Objeto();
                temp.setNombre(result.getString("nombre"));
                temp.setDescripcion(result.getString("descripcion"));
                temp.setPropietario(result.getString("ID_usuario"));
                temp.setOID(result.getString("OID"));
                temp.setTipo(result.getString("tipo"));
                searchResults.add(temp);
            }
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            ConnectionManager.getInstance().checkIn(conn);
            try {
                if (result != null)
                    result.close();
                if (stmt != null)
                    stmt.close();
            } catch (SQLException e) {
            }

        }
        return searchResults;
    }

	public Objeto select(Connection conn, String OOID){
		PreparedStatement stmt = null;
        ResultSet result = null;
        Objeto p = null;
        String sql = "SELECT * FROM Product WHERE (OID = ?) ";

        try {
            stmt = (PreparedStatement)conn.prepareStatement(sql);
            stmt.setString(1, OOID);
            result = stmt.executeQuery();

            result.next();
            p = new Objeto();
            p.setDescripcion(result.getString("descripcion"));
            p.setPropietario(result.getString("ID_usuario"));
            p.setTipo(result.getString("tipo"));
            p.setNombre(result.getString("nombre"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return p;
		
	}
	public void delete(Connection conn, String OOID){
		
	}
	public void insert(Connection conn, String OOID, Objeto o){
		
	}

}
